package Chap1;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 



public class testdata {

	 public static void main(String[] args) {
	
		 
			
	

	//userD.setAddress1(addr);
//	vehicaldetail vh1=new vehicaldetail();
//	vh1.setVname("Honda");
// 
//	
//userD.setVehicaldetail(vh1);
	
	
	SessionFactory SessionFact=new Configuration().configure().buildSessionFactory();
Session sess1 = SessionFact.openSession();
	sess1.beginTransaction();
	
	for(int i=1;i<11;i++) // add 10 guys ajay1 ajay2 ..
	{
	Userdetail userD=new Userdetail();
	userD.setUsername("ajay c"+i);
	sess1.save(userD);
	}
	
	
//	sess1.save(vh1);
	sess1.getTransaction().commit();
	sess1.close();


//	 Userdetail u1=(Userdetail) sess1.get(Userdetail.class, 6);
//	System.out.println( "person name at 6 is "+u1.getUsername());
//	 //System.out.println( "person name at 6 is "+(sess1.get(Userdetail.class, 6)).getClass());
	
	 
	 System.out.println( "Select All===================================================================");
		
	 sess1 = SessionFact.openSession();
	 sess1.beginTransaction();	
	 String hql = "FROM Userdetail";
		Query query = sess1.createQuery(hql);
		List results = query.list();
		Iterator it = results.iterator();

		while (it.hasNext()) {

			Userdetail u1 = (Userdetail) it.next();

			System.out.print(u1.getUserid());

			System.out.println(u1.getUsername());

		}
	// sess1.close();
	 
	 System.out.println( "Select one===================================================================");
	
	 Session sess2 = SessionFact.openSession();
	 sess2.beginTransaction();	
	 String hql1 = "FROM Userdetail where userid=5";
		Query query1 = sess1.createQuery(hql1);
		List results1 = query1.list();
		Iterator it1 = results1.iterator();

		while (it1.hasNext()) {

			Userdetail u1 = (Userdetail) it1.next();

			System.out.print(u1.getUserid());

			System.out.println(u1.getUsername());

		}
	 sess2.close();
	 }	 
	 
}
